﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities.Admin;

namespace SMS.DAL
{
    public class AdminDA:BaseDA<ApplicationUser>
    {
        public override ApplicationUser SaveObject(string userName, ApplicationUser tObject)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteObject(string userName, ApplicationUser tObject)
        {
            throw new NotImplementedException();
        }

        public override List<ApplicationUser> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            throw new NotImplementedException();
        }

        public override List<ApplicationUser> SearchObject(string userName, string SearchText)
        {
            throw new NotImplementedException();
        }

        public int ActivateDeActivateUserLogin(string username, string mode)
        {
            /* Procedure
             
ALTER PROC [dbo].[ACTIVATE_DEACTIVATE_USERLOGIN] --4,'ACTIVATE'
(
@USERNAME VARCHAR(50),
@MODE VARCHAR(50)
)
AS 
BEGIN
IF(@MODE='ACTIVATE')
	UPDATE LOGINDETAIL SET ISLIVE=1 WHERE USERNAME=@USERNAME
ELSE IF(@MODE='DEACTIVATE')
	UPDATE LOGINDETAIL SET ISLIVE=0 WHERE USERNAME=@USERNAME

END        */
            try
            {                
                dbHelper.AddParameter("@USERNAME", username);
                dbHelper.AddParameter("@MODE", mode);
                int ind = dbHelper.ExecuteNonQuery("ACTIVATE_DEACTIVATE_USERLOGIN", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                if (ind > 0 && mode == "ACTIVATE")
                    LogActivity(username, string.Format("User activated Successfully{0}", username), null);
                else
                    LogActivity(username, string.Format("User De-Activated Successfully{0}", username), null);
                return ind;
            }
            catch (Exception ex)
            {
                LogException(username, ex, null);
                throw new Exception("Error Occured while saving..");
            }
        }

        public int ManageUserRoleSetting(string username, int role)
        {
            /* Procedure
             
CREATE PROC [dbo].[MANAGE_USER_ROLE] 
(
@USERNAME VARCHAR(50),
@ROLE INT
)
AS 
BEGIN
   UPDATE LOGINDETAIL SET ROLE=@ROLE WHERE USERNAME=@USERNAME
END
             
            */
            try
            {
                dbHelper.AddParameter("@USERNAME", username);
                dbHelper.AddParameter("@ROLE", role);
                int ind = dbHelper.ExecuteNonQuery("MANAGE_USER_ROLE", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                if (ind > 0)
                    LogActivity(username, string.Format("User Access role change Successfully {0}", username), null);                
                return ind;
            }
            catch (Exception ex)
            {
                LogException(username, ex, null);
                throw new Exception("Error Occured while saving..");
            }
        }
    }
}
